package main

import (
	"io"
	"log"
	"os"
	"time"
)

const (
	DATETIME_PATTERN = "2006-01-02 15:04:05"
	DATE_PATTERN     = "2006-01-02"
)

var (
	Trace *log.Logger // 输出所有信息
	Info  *log.Logger // 输出重要信息
	Warn  *log.Logger // 输出警告信息
	Error *log.Logger // 输出错误信息
)

func init() {
	filename := "./log/" + time.Now().Format(DATE_PATTERN) + ".txt"

	file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		log.Fatalln("无法打开日志文件！", err)
	}

	// log.SetOutput(file)
	// log.SetPrefix("demo: ")
	// log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile)
	// log.SetFlags(log.LstdFlags | log.Lshortfile)

	flag := log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile

	Trace = log.New(io.Discard, "TRACE: ", flag)
	Info = log.New(os.Stdout, "INFO: ", flag)
	Warn = log.New(os.Stdout, "WARN: ", flag)
	Error = log.New(io.MultiWriter(file, os.Stderr), "ERROR: ", flag)
}

func main() {
	// log.Println("我是日志")
	// log.Fatalln("我是日志")
	// log.Panicln("我是日志")

	Trace.Println("鸡毛蒜皮的小事")
	Info.Println("一些重要的信息")
	Warn.Println("这是一个警告信息")
	Error.Println("出现了错误")
}
